from django.db import models
from django.contrib.auth.models import User


class Property(models.Model):
    """
    Tag Property Types
    """
    name = models.TextField()

    def __str__(self):
        return self.name

class Tag(models.Model):
    """
    Tags
    """
    owner = models.ForeignKey(User)
    name = models.TextField()
    properties = models.ManyToManyField(Property)

    def __str__(self):
        return "%s" % self.name

class Recurring(models.Model):
    """
    Recurring
    """
    days = models.TextField()

    def __str__(self):
        return "%s" % self.days

class Filter(models.Model):
    """
    User saved filters
    """
    
    owner = models.ForeignKey(User)
    name = models.TextField()
    tags = models.ManyToManyField(Tag)    # AND Logic for selection
    completed = models.BooleanField()     # Show completed items?
    flagged = models.BooleanField()       # Show only flagged items?


    def __str__(self):
        return self.name


class SavedView(models.Model):
    """
    User saved collections of filters
    """
    
    name = models.TextField()
    owner = models.ForeignKey(User)
    filters = models.ManyToManyField(Filter)

    def __str__(self):
        return self.name


class Item(models.Model):
    """
    Action Items
    """

    name = models.TextField()
    owner = models.ForeignKey(User)
    effort = models.IntegerField(blank=True)
    priority = models.IntegerField()
    flag = models.BooleanField()
    notes = models.TextField(blank=True)
    
    tags = models.ManyToManyField(Tag)
    
    recurring = models.ForeignKey(Recurring)
    
    due = models.DateTimeField(null=True, blank=True)
    start = models.DateTimeField(null=True, blank=True)
    complete = models.BooleanField()
    source = models.TextField()
    
    def __str__(self):
        return "%s by %s (%s - %s)" % (self.name, self.owner.email, self.start, self.due) 
